<template>
	<view>
		<!-- 自定义头部导航 -->
		<!-- <page-meta>
			<navigation-bar :title="'原材料收货'" front-color="#000000" background-color="#FBFBFF" fixed />
		</page-meta> -->
		<view class="page">
			<!-- <view class="upload-scan">
				<label style="margin-right: 5px;">扫描包装条码</label>
				<scan-bar style="display: inline-block" id="scanInput" ref="scanBarRef" placeholder="扫描包装条码"
					@code-scanned="getScanResult"></scan-bar>
			</view> -->
			<view class="area-header">
				<u-text :text="'已扫描工序流转单'+' (' + scannedList.length+')'">
				</u-text>
			</view>
			<view class="list-content">
				<!-- 列表 -->
				<u-list class="list" v-if="scannedList && scannedList.length > 0" :height="ListViewHeight">
					<u-list-item v-for="(item, index) in scannedList" :key="item.id">
						<view class="card">
							<view class="right">
								<view class="title">
									工序流转单: {{item.externalCode}}
								</view>
								<view class="body">
									工作中心:{{ item.workCenterCode }}-{{ item.workCenterName }}
								</view>
								<view class="body">
									<label class="span">批次号:{{ item?.remarks || '待定'}}</label>
								</view>
								<view class="body">
									<label>物料:{{ item.materialCode }}-{{ item.materialName }}</label>
								</view>
								<view class="body">
									<label class="span">数量: {{item?.productQuantity || '待定'}}</label>
								</view>
							</view>
							<view class="left" v-if="exteriorDelivery.deliveryState == 0">
								<u-icon name="close-circle-fill" color="#f56c6c" size="30px"
									@click="removeDetail(item,index)"></u-icon>
							</view>
						</view>

						<u-line></u-line>
					</u-list-item>
				</u-list>
				<!-- 空数据 -->
				<view v-else>
					<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
				</view>
			</view>
			<u-modal :show="messageShow" :closeOnClickOverlay="false" title="提示" content="是否要删除此包装"
				:showCancelButton="true" @confirm="messageConfirm"
				@cancel="messageCancel"></u-modal>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { computed, ref } from 'vue';
	import ScanBar from '@/components/scan-bar/index.vue';
	import { onShow, onPullDownRefresh } from '@dcloudio/uni-app';
	import { http } from "../../../utils/http";
	import { useUserStore } from '../../../stores/useUserStore';

	// 重置表单信息 & 光标自动blur
	const scanBarRef = ref();
	const ListViewHeight = ref(0);
	const messageShow = ref(false);
	const deleteIndex = ref(0);
	// 登录工作组信息
	//const loginWorkInfo = computed(() => JSON.parse(useUserStore().loginWorkInfo));
	//console.log(loginWorkInfo.value)

	const exteriorDelivery = ref()
	const props = defineProps({
		result: {
			type: String,
		},
	});
	exteriorDelivery.value = JSON.parse(props.result)
	//已扫描集合
	const scannedList : any = ref([])
	onShow(async () => {
		ListViewHeight.value = uni.getSystemInfoSync().windowHeight - 160;
		console.log('ListViewHeight.value', ListViewHeight.value);
		getDetail()
	})
	onPullDownRefresh(() => {
		scannedList.value = [];
		getDetail('refresh')
	});
	// 获取扫码结果
	async function getScanResult(result : string) {
		if (!result) {
			return
		}
		console.log('扫码结果', result);
		getDetail(null, result)
	}

	//查询明细信息
	async function getDetail(type ?: string, result ?: string) {
		const res = await http({
			method: 'GET',
			url: `/mes/mesexteriordelivery/get/${exteriorDelivery.value.id}`,
		});
		console.log(res.data)
		scannedList.value = res.data.mesProduceTransferFormDTOS
		type === 'refresh' && uni.stopPullDownRefresh();
	}

	async function messageConfirm() {
		messageShow.value = false;
		scannedList.value.splice(deleteIndex.value, 1);
		deleteById()
	}

	function messageCancel() {
		messageShow.value = false;
	}
	const deliveryUnit = ref()
	function removeDetail(item : any, index : any) {
		deleteIndex.value = index;
		messageShow.value = true;
		deliveryUnit.value = item;

	}
	async function deleteById() {
		await http({
			method: 'POST',
			url: `/mes/mesexteriordeliveryunit/delete`,
			data: { ids: [deliveryUnit.value.id] }
		}).then((res) => {
			if (res.code == "200") {
				uni.$u.toast('删除成功！');
			}
		});
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		flex-direction: column;
	}

	.upload-scan {
		padding: 10px 40rpx 10px 40rpx;
	}

	.area-header {
		color: #909193;
		padding: 10px 15px;
		margin-bottom: 0;
		font-size: 15px;
		background-color: #f3f4f6;
		display: flex;
	}

	.list-content {
		padding: $uni-page-padding;

		.list {
			.card {
				margin-top: 5px;
				margin-bottom: 10px;
				border-radius: 7px;
				display: flex;
				align-items: center;
				padding: 10px 0;
				transition: all 300ms ease;
				box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);

				.left {
					// flex-basis: 90rpx;
					// padding-right: 10px;
					text-align: right;
					font-size: 13px;

					.enter {
						line-height: 20px;
						margin-bottom: 5px;
					}

					.number {
						font-size: 13px;
						line-height: 18px;
					}

					:deep(.u-icon--right) {
						flex-direction: row-reverse;
					}
				}

				.right {
					flex: 1;
					padding-left: 15px;

					.title {
						font-size: 18px;
						line-height: 20px;
						margin-bottom: 5px;
						font-weight: 700;
					}

					.body {
						font-size: 16px;
						line-height: 18px;

						.span {
							width: 50%;
							// text-align: left;
							display: inline-block;
						}
					}
				}
			}
		}
	}

	.search {
		display: flex;
		flex: 1;

		.custom-style {
			// width: 120px;
			margin: 12px 5px 5px;
			height: 40px;
			border-radius: 8px;

			&:nth-child(1) {
				flex: 1
			}

			&:nth-child(2) {
				flex: 3
			}
		}
	}
</style>